/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Role;
import org.crystalrange.service.RoleService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class RoleListController extends BaseController {

    protected RoleService roleService;
    protected Role selectedRole;
    protected Listbox listboxRole;
    protected Search searchRole;
    protected List<Role> roles;

    public RoleService getRoleService() {
        return this.roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public Role getSelectedRole() {
        return this.selectedRole;
    }

    public void setSelectedRole(Role selectedRole) {
        this.selectedRole = selectedRole;
    }

    public List<Role> getRoles() {
        return this.roles;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchRole = new Search(Role.class);
        this.searchRole.setMaxResults(100);
        this.searchRole.addFetch("createdBy");
        this.searchRole.addFetch("updatedBy");
        this.roles = this.roleService.search(this.searchRole);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemRoleCreate(Event event) throws Exception {
        this.roleCreate();
    }

    public void onClick$menuitemRoleEdit(Event event) throws Exception {
        this.roleEdit();
    }

    public void onClick$menuitemRoleDelete(Event event) throws Exception {
        this.roleDelete();
    }

    public void onClick$menuitemRoleSearch(Event event) throws Exception {
        this.roleSearch();
    }

    public void onClick$menuitemRoleRefresh(Event event) throws Exception {
        this.roleRefresh();
    }

    public void onClick$toolbarbuttonRoleCreate(Event event) throws Exception {
        this.roleCreate();
    }

    public void onClick$toolbarbuttonRoleEdit(Event event) throws Exception {
        this.roleEdit();
    }

    public void onClick$toolbarbuttonRoleDelete(Event event) throws Exception {
        this.roleDelete();
    }

    public void onClick$toolbarbuttonRoleSearch(Event event) throws Exception {
        this.roleSearch();
    }

    public void onClick$toolbarbuttonRoleRefresh(Event event) throws Exception {
        this.roleRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.roleEdit();
    }
    
    private void roleCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/role.zul");
    }

    private void roleEdit() throws Exception {
        if (this.selectedRole != null) {
            this.execution.sendRedirect("~./zul/html/role.zul?Id=" + this.selectedRole.getId().toString());
        }
    }

    private void roleDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxRole.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Role role = (Role) selectedItem.getValue();
                    this.roleService.remove(role);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.roles = this.roleService.search(this.searchRole);
        this.binder.loadAll();
    }

    private void roleSearch() throws Exception {
        this.execution.setAttribute("searchRole", this.searchRole);

        Window window = (Window) this.execution.createComponents("~./zul/html/roleSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchRole = (Search) this.execution.getAttribute("searchRole");
        }

        this.roles = this.roleService.search(this.searchRole);
        this.binder.loadAll();
    }

    private void roleRefresh() throws Exception {
        this.roles = this.roleService.search(this.searchRole);
        this.binder.loadAll();
    }
}
